package com.yh.jandan.interceptor ;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.yh.jandan.controller.JandanController;

public class SecureInterceptor implements Interceptor {

	public void intercept ( Invocation inv ) {
		if ( isLogin ( inv.getController ( ) ) // /
				|| doLogin ( inv.getActionKey ( ) ) ) {
			inv.invoke ( ) ;
		} else {
			System.out.println ( "SecureInterceptor:[" + inv.getActionKey ( ) + "] error" ) ;
			inv.getController ( ).redirect ( "/" ) ;
		}
	}

	private boolean doLogin ( String actionKey ) {
		return actionKey.toLowerCase ( ).indexOf ( "login" ) > -1 ;
	}

	private boolean isLogin ( Controller controller ) {
		Object o = controller.getSession ( ).getAttribute ( JandanController.key ) ;
		return o != null && !o.toString ( ).equals ( "" ) ;
	}

}
